package com.zhs.gccs.utils;


import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zhs.gccs.base.App;
import com.zhs.gccs.model.SmTask;
import com.zhs.gccs.model.TaskProject;

import java.util.List;


public class SharedPrefHelper {
    public static String key_user_name = "key_username";
    ///////////////////////////////////////
    public static String key_catypes = "key_catypes";
    public static String key_record_gj = "record_gj_group";
    public static String key_record_gj_group_id = "record_gj_group_id";
    public static String key_record_gj_id_pre_point = "record_gj_id_pre_point";
    public static String key_record_gj_id_pre = "record_gj_id_pre";
    //////////////////////////////////////////////////////////////

    private static final String SP_FILE_NAME = "APPLICATION_SP";
    private static SharedPrefHelper sharedPrefHelper = null;
    private static SharedPreferences sharedPreferences;


    public static synchronized SharedPrefHelper getInstance() {
        if (null == sharedPrefHelper) {
            sharedPrefHelper = new SharedPrefHelper();
        }
        return sharedPrefHelper;
    }

    private SharedPrefHelper() {
        sharedPreferences = App.getApplication().getApplicationContext().getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
    }

    public void setArrayData(String key, List data) {
        sharedPreferences.edit().putString(key, JSON.toJSONString(data)).commit();
    }

    public List getArrayData(String key, Class clazz) {
        String data = sharedPreferences.getString(key, null);
        if (TextUtils.isEmpty(data)) {
            return null;
        }
        return JSON.parseArray(data, clazz);
    }

    public void setBoolean(String key,boolean val) {
        sharedPreferences.edit().putBoolean(key, val).commit();
    }

    public boolean getBoolean(String key,boolean defaultVal) {
        return sharedPreferences.getBoolean(key, defaultVal);
    }

    public void setString(String key,String val) {
        sharedPreferences.edit().putString(key, val).commit();
    }

    public String getString(String key,String defaultVal) {
        return sharedPreferences.getString(key,defaultVal);
    }


    /**
     * 是否第一次启动app
     */
    public void setIsFirst(boolean isFirst) {
        sharedPreferences.edit().putBoolean("isFirst", isFirst).commit();
    }

    public boolean getIsFirst() {
        return sharedPreferences.getBoolean("isFirst", true);
    }


    public void setToken(String token) {
        sharedPreferences.edit().putString("token", token).commit();
    }

    public String getToken() {
        return sharedPreferences.getString("token", null);
    }


    /**
     * 保存登录账号
     */
    public void setLoginAccount(String mobile) {
        sharedPreferences.edit().putString("loginAccount", mobile).commit();
    }

    //用户登录账号
    public String getLoginAccount() {
        return sharedPreferences.getString("loginAccount", null);
    }

    public void setLastLoginTime(long time) {
        sharedPreferences.edit().putLong("LastLoginTime", time).commit();
    }

    //用户登录账号
    public long getLastLoginTime() {
        return sharedPreferences.getLong("LastLoginTime", 0);
    }

    //用户
    public void setUserNickName(String userName) {
        sharedPreferences.edit().putString("UserNickName", userName).commit();
    }

    public String getUserNickName() {
        return sharedPreferences.getString("UserNickName", null);
    }

    public void setUserAccount(String userName) {
        sharedPreferences.edit().putString("UserAccount", userName).commit();
    }

    public String getUserAccount() {
        return sharedPreferences.getString("UserAccount", null);
    }

    public void setUserOrgName(String userName) {
        sharedPreferences.edit().putString("UserOrgName", userName).commit();
    }

    public String getUserOrgName() {
        return sharedPreferences.getString("UserOrgName", null);
    }

    public void setUserXzq(String xzq) {
        sharedPreferences.edit().putString("UserXzq", xzq).commit();
    }

    public String getUserXzq() {
        return sharedPreferences.getString("UserXzq", null);
    }


    /**
     * 任务截图路径
     */
    public void setTaskImgUrl(String taskId, String isReady) {
        sharedPreferences.edit().putString("TaskImgUrl" + taskId, isReady).commit();
    }

    public String getTaskImgUrl(String taskId) {
        return sharedPreferences.getString("TaskImgUrl" + taskId, "");
    }

    /**
     * 任务轨迹截图路径
     */
    public void setTaskGJImgUrl(String taskId, String isReady) {
        sharedPreferences.edit().putString("TaskGJImgUrl" + taskId, isReady).commit();
    }

    public String getTaskGJImgUrl(String taskId) {
        return sharedPreferences.getString("TaskGJImgUrl" + taskId, "");
    }

    /**
     * 保存登录账号的时间
     */
    public void setLoginData(String mobile) {
        sharedPreferences.edit().putString("loginData", mobile).commit();
    }

    public String getLoginData() {
        return sharedPreferences.getString("loginData", null);
    }


    public void setCurrentTask(SmTask smTask) {
        sharedPreferences.edit().putString("task", JSON.toJSONString(smTask)).commit();
    }

    public SmTask getCurrentTask() {
        String task = sharedPreferences.getString("task", null);
        if (TextUtils.isEmpty(task)) return null;
        return JSON.parseObject(task, SmTask.class);
    }


    public void setCurrentProject(TaskProject taskProject) {
        sharedPreferences.edit().putString("taskProjectCurrent", JSON.toJSONString(taskProject)).commit();
    }

    public TaskProject getCurrentProject() {
        String project = sharedPreferences.getString("taskProjectCurrent", null);
        if (TextUtils.isEmpty(project)) return null;
        return JSON.parseObject(project, TaskProject.class);
    }

    /**
     * 二次核查数据时间戳
     */
    public void setFeedbackSecondTime(String FeedbackSecondTime) {
        sharedPreferences.edit().putString("FeedbackSecondTime", FeedbackSecondTime).commit();
    }

    public String getFeedbackSecondTime() {
        return sharedPreferences.getString("FeedbackSecondTime", "");
    }


    /**
     * 解压密码
     */
    public void setUnZipPWD(String UnZipPWD) {
        sharedPreferences.edit().putString("UnZipPWD", UnZipPWD).commit();
    }

    public String getUnZipPWD() {
        return sharedPreferences.getString("UnZipPWD", "");
    }


    public void setTakeImageFileDir(String file) {
        sharedPreferences.edit().putString("TakeImageFile", file).commit();
    }

    public void setTakeImageFilePath(String file) {
        sharedPreferences.edit().putString("TakeImageFilePath", file).commit();
    }

    public String getTakeImageFilePath() {
        return sharedPreferences.getString("TakeImageFilePath", null);
    }

    public String getTakeImageFileDir() {
        return sharedPreferences.getString("TakeImageFile", null);
    }

    public void setTakeImageFileNameNoExt(String fileName) {
        sharedPreferences.edit().putString("TakeImageFileName", fileName).commit();
    }

    public String getTakeImageFileNameNoExt() {
        return sharedPreferences.getString("TakeImageFileName", null);
    }

    public void setTakeVideoFileDir(String file) {
        sharedPreferences.edit().putString("TakeVideoFile", file).commit();
    }

    public String getTakeVideoFileDir() {
        return sharedPreferences.getString("TakeVideoFile", null);
    }

    public void setTakeVideoFileNameNoExt(String fileName) {//不带ext
        sharedPreferences.edit().putString("TakeVideoFileName", fileName).commit();
    }

    public String getTakeVideoFileNameNoExt() {
        return sharedPreferences.getString("TakeVideoFileName", null);
    }

    public void setTakeAudioFileDir(String file) {
        sharedPreferences.edit().putString("TakeAudioFile", file).commit();
    }

    public String getTakeAudioFileDir() {
        return sharedPreferences.getString("TakeAudioFile", null);
    }

    public void setTakeAudioFileNameNoExt(String fileName) {
        sharedPreferences.edit().putString("TakeAudioFileName", fileName).commit();
    }

    public String getTakeAudioFileNameNoExt() {
        return sharedPreferences.getString("TakeAudioFileName", null);
    }

    public void setCurrentPhotoVideoAudioFor(String selectedTag) {
        sharedPreferences.edit().putString("CurrentPhotoVideoAudioFor", selectedTag).commit();
    }

    public String getCurrentPhotoVideoAudioFor() {
        return sharedPreferences.getString("CurrentPhotoVideoAudioFor", "");
    }

    public void setCurrentPhotoVideoAudioRemark(String remark) {
        sharedPreferences.edit().putString("CurrentPhotoVideoAudioRemark", remark).commit();
    }

    public String getCurrentPhotoVideoAudioRemark() {
        return sharedPreferences.getString("CurrentPhotoVideoAudioRemark", "");
    }


    public void setUserRoles(List<String> roles) {
        sharedPreferences.edit().putString("userRoles", JSON.toJSONString(roles)).commit();
    }

    public List<String> getUserRoles() {
        return JSONArray.parseArray(sharedPreferences.getString("userRoles", null), String.class);
    }

    public void setTaskFile(String fileName, boolean download) {
        sharedPreferences.edit().putBoolean(fileName, download).commit();
    }

    public boolean getTaskFile(String fileName) {
        return sharedPreferences.getBoolean(fileName, false);
    }


    public void setTaskUploaded(String taskId, boolean val) {
        sharedPreferences.edit().putBoolean("TaskUploaded_" + taskId, val).commit();
    }

    public boolean getTaskUploaded(String taskId) {
        return sharedPreferences.getBoolean("TaskUploaded_" + taskId, false);
    }

    public void setTaskDownload(String taskId, boolean val) {
        sharedPreferences.edit().putBoolean("TaskDownload_" + taskId, val).commit();
    }

    public boolean getTaskDownload(String taskId) {
        return sharedPreferences.getBoolean("TaskDownload_" + taskId, false);
    }


    public void setTaskProjectInfoSync(String taskId, boolean val) {
        sharedPreferences.edit().putBoolean("TaskProjectSync_" + taskId, val).commit();
    }

    public boolean getTaskProjectInfoSync(String taskId) {
        return sharedPreferences.getBoolean("TaskProjectSync_" + taskId, false);
    }


    public boolean getTaskAttachSync(String taskId) {
        return sharedPreferences.getBoolean("taskAttachSync" + taskId, false);
    }

    public void setTaskAttachSync(String taskId, boolean val) {
        sharedPreferences.edit().putBoolean("taskAttachSync" + taskId, val).commit();
    }


}
